package com.dk.mq;

import com.dk.pojo.TSpike;
import com.dk.pojo.TSpikeOrder;
import com.dk.service.ITSpikeOrderService;
import com.dk.service.ITSpikeService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import util.IdWorker;

@RocketMQMessageListener(consumerGroup = "my-group",topic = "seckill-goods")
@Component
public class TSpikeMQ implements RocketMQListener<String> {

    @Autowired
    private ITSpikeService itSpikeService;

    @Autowired
    private ITSpikeOrderService orderService;

    @Autowired
    private RedisTemplate redisTemplate;

    //增加秒杀订单
    @Override
    public void onMessage(String str) {
        String[] split = str.split(",");
        Long goodsId = Long.parseLong(split[1]);
        TSpike tSpike = itSpikeService.getById(goodsId);

        //创建订单
        IdWorker idWorker = new IdWorker();
        long orderId = idWorker.nextId();
        TSpikeOrder order = new TSpikeOrder();

        //订单号
        order.setOutTradeNo(orderId + "");
        //商品id
        order.setSeckillId(goodsId);
        //用户id
        order.setAdminId(Long.valueOf(split[2]));
        //价格
        order.setMoney(tSpike.getCostPrice());
        //状态
        order.setStatus("0");

        orderService.save(order);
        redisTemplate.boundHashOps("orderId").put(split[0],goodsId);
    }
}
